package com.example.myapp;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

public class UpdateActivity extends AppCompatActivity {
    EditText age_input,height_input,weight_input;
    RadioGroup sex_input,type_input;
    Button update_button,delete_button;
    RadioButton male_input,female_input,never,min,mid,max;
    String id,age,weight,height,sex,type;
    String str,str_type;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update);

        //获取布局中的组件
        age_input = findViewById(R.id.age_edit2);
        height_input = findViewById(R.id.height_edit2);
        weight_input = findViewById(R.id.weight_edit2);
        sex_input = findViewById(R.id.sex_radio2);
        type_input = findViewById(R.id.type_radio2);
        male_input = findViewById(R.id.male2);
        female_input = findViewById(R.id.female2);

        never = findViewById(R.id.never2);
        min = findViewById(R.id.min2);
        mid = findViewById(R.id.mid2);
        max = findViewById(R.id.max2);



        //调用方法
        getAndsetIntentData();

        //设置最上方标题
        ActionBar ab = getSupportActionBar();
        ab.setTitle("day"+id);


        //点击更新按钮实现更新
        update_button = findViewById(R.id.update_btn2);
        update_button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {

                if(male_input.isChecked()==true){
                    str=male_input.getText().toString();

                }else if (female_input.isChecked()==true){
                    str=female_input.getText().toString();
                }
                if (max.isChecked()==true){
                    str_type = max.getText().toString();
                }
                else if (min.isChecked()==true){
                    str_type = min.getText().toString();
                }
                else if (mid.isChecked()==true){
                    str_type = mid.getText().toString();
                }
                else {
                    str_type = never.getText().toString();
                }

                MyDatabaseHelper myDB = new MyDatabaseHelper(UpdateActivity.this);
                age = age_input.getText().toString().trim();
                weight = weight_input.getText().toString().trim();
                height = height_input.getText().toString().trim();
                sex = str;
                type = str_type;
                myDB.updateData(id,age,height,weight,sex,type);
                finish();

            }
        });
        //删除按钮事件
        delete_button = findViewById(R.id.delete_btn2);
        delete_button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                confirmDialog();
            }
        });


    }


    //获取从CustomAdapter储存在intent里的数据
    void getAndsetIntentData(){
        if(getIntent().hasExtra("id")&&
                getIntent().hasExtra("age")&&
                getIntent().hasExtra("height")&&
                getIntent().hasExtra("weight")){
            //从intent中获取数据
            id = getIntent().getStringExtra("id");
            age = getIntent().getStringExtra("age");
            height = getIntent().getStringExtra("height");
            weight = getIntent().getStringExtra("weight");
            //将从intent处获得的数据显示在页面上
            age_input.setText(age);
            height_input.setText(height);
            weight_input.setText(weight);
        }
        else {
            Toast.makeText(this,"no data",Toast.LENGTH_SHORT).show();
        }
    }
    void confirmDialog(){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("状态删除");
        builder.setMessage("确定要删除本次状态吗");
        builder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                MyDatabaseHelper myDB = new MyDatabaseHelper(UpdateActivity.this);
                myDB.deleteOneRow(id);
                finish();
            }
        });
        builder.setNegativeButton("no", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
            }
        });
        builder.create().show();
    }
}

